 #include "StateGame.h"
#include "CDXManager.h"
#include "Macros.h"
#include "CVSView.h"
#include <windows.h>
#define SPEED 2;

StateGame::StateGame():CState()
{
}
void StateGame::Init()
{
	m_pSprite=new CSprite(MARIO);
	m_pAnimation=new Animation(8,3,0.025,m_pSprite);
	ErrorTrace(L"State Game!:Init");
	m_pBackGr=CDXManager::GetInstance()->LoadSurface(GAME);
	sound=new CSound(CVSView::GetInstance()->GethWnd());
	if (!sound->init(-1, 44100, 0))
	{
		ErrorTrace(L"sound is not init.");
	}
	if (!sound->addFile(STR_MOD, "mysound", "M02.MOD", BASS_SAMPLE_LOOP))
	{
		printf("sound is not add");
	}
	
	sound->play("mysound", true);
	v=0;
	x=0;
	y=0;
}

void StateGame::Update()
{
	ErrorTrace(L"State Game!:Update");
	CDXManager::GetInstance()->ProcessKeyBoard();
	ProcessInput(CDXManager::GetInstance()->GetDevice(),CFpsController::GetInstance()->GetFrameDt());

}

void StateGame::Render()
{
	ErrorTrace(L"State Game!:Render");
	CDXManager::GetInstance()->BeginRender();
	CDXManager::GetInstance()->RenderSurface();
	CDXManager::GetInstance()->GetDXSprite()->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_DEPTH_FRONTTOBACK);
	m_pSprite->Draw(x,y,1.0f,1.0f,D3DXVECTOR2(0,0),0,D3DCOLOR_XRGB(255,255,255),0,m_pAnimation);
	CDXManager::GetInstance()->GetDXSprite()->End();
	CDXManager::GetInstance()->EndRender();
}

void StateGame::Exit()
{	
	if (m_pBackGr)
	{
		m_pBackGr->Release();
		m_pBackGr=NULL;
	}
	sound->deinitialize();
	ErrorTrace(L"State Game:Exit");
}

void StateGame::ProcessInput( LPDIRECT3DDEVICE9 d3ddv, float Delta )
{	
	if (CDXManager::GetInstance()->IsKeyDown(DIK_RIGHT))
	{
		x+=SPEED;
		m_pAnimation->Update(0,5,Delta);
		ErrorTrace(L"RightKey press!");
	}	
	else 
		if (CDXManager::GetInstance()->IsKeyDown(DIK_LEFT))
		{
			x-=SPEED;
			m_pAnimation->Update(8,13,Delta);
			ErrorTrace(L"LeftKey press!");
		}
		else 
		{

		}
}


void StateGame::OnKeyDown( int KeyCode )
{
	switch (KeyCode)
	{
	case DIK_ESCAPE:
		break;
	}
}

void StateGame::OnKeyUp( int KeyCode )
{

}
